<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="风机名称" prop="equName">
        <el-input
          v-model="queryParams.equName"
          placeholder="请输入风机名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content-container height_100-query_from">
      <div class="handle_bar">
        <div class="tit"><span>应用列表</span></div>
        <el-button
          v-hasPermi="['pilot_lamp:list:add']"
          type="primary"
          plain
          size="small"
          @click="handleAdd"
        >添加</el-button>
      </div>
      <div class="flex-1" style="position: relative;overflow: hidden;">
        <div style="position: absolute;left: 0;top: 0;bottom: 0;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="loading"
            class="flex-1"
            height="250"
            style="width:100%;"
            :data="equList"
            @selection-change="handleSelectionChange"
          >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <!-- <el-table-column label="ID" align="center" prop="id" /> -->
            <el-table-column label="设备名称" align="center" prop="equName" />
            <el-table-column label="安装位置" align="center" prop="locationName" />
            <!-- <el-table-column label="生产厂家" align="center" prop="produceCompany" />
            <el-table-column label="供应商" align="center" prop="supplier" />
            <el-table-column label="规格型号" align="center" prop="brand" />
            <el-table-column label="购入价格" align="center" prop="price" />
            <el-table-column label="生产日期" align="center" prop="produceDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="质保期" align="center" prop="warranty" />-->
            <el-table-column label="ip地址" align="center" prop="ip" />
            <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  v-hasPermi="['pilot_lamp:list:edit']"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >编辑</el-button>
                <el-button
                  v-hasPermi="['pilot_lamp:list:del']"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改设备定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
        <el-form-item label="设备名称" prop="equName">
          <el-input v-model="form.equName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="安装位置" prop="locationId">
          <el-select v-model="form.locationId" placeholder="请选择上/下行" style="width: 100%;">
            <el-option
              v-for="tunnel in tunnelAll"
              :key="tunnel.id"
              :label="tunnel.tunnelName"
              :value="tunnel.id"
            >
              <span style="float: left">{{ tunnel.tunnelName }}（{{ tunnel.direction }}）</span>
              <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ tunnel.direction }}</span> -->
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="ip地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入ip地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- <PLC ref="plc" /> -->
  </div>
</template>

<script>
// import PLC from './PLC/index.vue'
import { listEqu, getEqu, delEqu, addEqu, updateEqu } from '@/api/fjgs/equ'
import { listAll } from '@/api/system/tunnel'

export default {
  name: 'Equ',
  components: {
    // PLC
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 10,
      // 设备定义表格数据
      equList: [],
      tunnelAll: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equName: null,
        locationName: null,
        produceCompany: null,
        supplier: null,
        brand: null,
        price: null,
        produceDate: null,
        warranty: null,
        ip: null,
        lightType: null,
        equType: 3
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
    listAll().then(res => {
      this.tunnelAll = res.data
    })
  },
  methods: {
    /** 查询设备定义列表 */
    getList() {
      this.loading = true
      listEqu(this.queryParams).then(response => {
        this.equList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equName: null,
        equType: 3
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加设备定义'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEqu(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改设备定义'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEqu(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEqu(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除设备定义编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delEqu(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lc/equ/export', {
        ...this.queryParams
      }, `lc_equ.xlsx`)
    }
  }
}
</script>

